#include "OpeningScreen.h"


LRESULT CALLBACK
MainWndProcOpen(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	static OpeningScreen* app = 0;

	switch( msg )
	{
	case WM_CREATE:
		{
			// Get the 'this' pointer we passed to CreateWindow via the lpParam parameter.
			CREATESTRUCT* cs = (CREATESTRUCT*)lParam;
			app = (OpeningScreen*)cs->lpCreateParams;
			return 0;
		}
	}

	// Don't start processing messages until after WM_CREATE.
	if( app )
		return app->msgProc(msg, wParam, lParam);
	else
	{
		return DefWindowProc(hwnd, msg, wParam, lParam);
	}
}

//////////////////////////////////////////////////////////////////////////

OpeningScreen::OpeningScreen(void)
{
}


OpeningScreen::~OpeningScreen(void)
{
}



bool OpeningScreen::Init(HINSTANCE hInstance)
{
	WNDCLASS wc;
	int x, y;

	m_hAppInst = hInstance;

	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = MainWndProcOpen;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInstance;
	wc.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.lpszMenuName = 0;
	wc.lpszClassName = TEXT(SPLASHSCREEN_WINDOW_CLASS);

	if(!RegisterClass(&wc))
	{
		MessageBox(0, TEXT("Failed to register window class for splashscreen."), TEXT(SPLASHSCREEN_WINDOW_TITLE), MB_OK | MB_ICONERROR);
		return FALSE;
	}

	x = (GetSystemMetrics(SM_CXSCREEN) / 2) - (SPLASHSCREEN_IMAGE_WIDTH / 2);
	y = (GetSystemMetrics(SM_CYSCREEN) / 2) - (SPLASHSCREEN_IMAGE_HEIGHT / 2);

	m_hSplashBitmap = (HBITMAP)::LoadImage(NULL,
		L"./Resource/chess.bmp",
		IMAGE_BITMAP,
		0,
		0,
		LR_LOADFROMFILE); 

	if(!m_hSplashBitmap)
	{
		MessageBox(0, TEXT("Failed to load splashscreen image."), TEXT(SPLASHSCREEN_WINDOW_TITLE), MB_OK | MB_ICONERROR);
		return FALSE;
	}

	BITMAP bitmap;

	GetObject (m_hSplashBitmap, sizeof (BITMAP), &bitmap) ; 


	m_hMainWnd = CreateWindow(
		TEXT(SPLASHSCREEN_WINDOW_CLASS),
		TEXT(SPLASHSCREEN_WINDOW_TITLE),
		WS_POPUP|WS_DISABLED,
		x,
		y,
		bitmap.bmWidth,
		bitmap.bmHeight,
		NULL,
		NULL,
		m_hAppInst,
		this);

	if(!m_hMainWnd)
	{
		MessageBox(0, TEXT("Failed to create splashscreen window."), TEXT(SPLASHSCREEN_WINDOW_TITLE), MB_OK | MB_ICONERROR);
		return FALSE;
	}

	x = (GetSystemMetrics(SM_CXSCREEN) / 2) - (SPLASHSCREEN_IMAGE_WIDTH / 2);
	y = (GetSystemMetrics(SM_CYSCREEN) / 2) - (SPLASHSCREEN_IMAGE_HEIGHT / 2);

	//SetWindowPos(m_hMainWnd, HWND_TOPMOST, x, y, 400, 200, SWP_SHOWWINDOW);
	SetForegroundWindow(m_hMainWnd);
	SetFocus(m_hMainWnd);
	ShowWindow(m_hMainWnd, SW_SHOW);

	UpdateWindow(m_hMainWnd);
	BringWindowToTop(m_hMainWnd);

	return TRUE;
}

void OpeningScreen::DrawBitmap( HDC hDC, HBITMAP hBitmap, int x, int y )
{
	BITMAP bm;
	HDC hMemDC;
	GetObject(hBitmap, sizeof(BITMAP), &bm);
	hMemDC = CreateCompatibleDC(hDC);
	SelectObject(hMemDC, hBitmap);
	BitBlt(hDC, x, y, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCCOPY);
	DeleteDC(hMemDC);
}

LRESULT OpeningScreen::msgProc( UINT msg, WPARAM wParam, LPARAM lParam )
{
	HDC hDC;

	switch(msg)
	{
	case WM_PAINT:
		hDC = GetDC(m_hMainWnd);
		DrawBitmap(hDC, m_hSplashBitmap, 0, 0);
		ReleaseDC(m_hMainWnd, hDC);
		return 0;
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	}

	return DefWindowProc(m_hMainWnd, msg, wParam, lParam);
}

void OpeningScreen::Show()
{
	MSG msg;
	while(1)
	{

		if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
		{
			if(msg.message == WM_QUIT)
			{
				break;
			}

			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}

		if(GlobalMessage::Instance()->ResoueceIsReady())
			break;
	}
}

void OpeningScreen::Destroy()
{

	DeleteObject(m_hSplashBitmap);
	ShowWindow(m_hMainWnd,SW_HIDE);
	UnregisterClass(TEXT(SPLASHSCREEN_WINDOW_CLASS), m_hAppInst);
}
